var express = require('express');
var router = express.Router();
var dataModel = require('../models/data-model')

function getAnnouncemnets(req, res, next) {
  req.announce = []
  dataModel.readTable("announce", function(data) {
    req.announce = data
    next()
  })
}
function getItems(req, res, next) {
  req.items = []
  dataModel.readTable('items', function(data) {
    Object.keys(data).forEach(function(id) {
      let price = data[id].price
      if(price % 100) {
        price = (parseInt(price)/100).toFixed(2)
      } else {
        price = (parseInt(price)/100)
      }
      data[id].price = price
    })
    req.items = data
    next()
  })
}

function getEvents(req, res, next) {
  dataModel.readTable('events', function(data) {
    req.events = data
    next()
  })
}

function getMotd(req, res, next) {
  dataModel.readTable('motd', function(data) {
    req.motd = data
    next()
  })
}

function renderPage(req, res) {
  res.render('index', {
    pagetile: '首页',
    announcements: req.announce,
    items: req.items,
    events: req.events,
    motd: req.motd
  })
}
router.get('/', getAnnouncemnets,
  getItems,
  getEvents,
  getMotd,
  renderPage
)

module.exports = router;
